package games.graphPath;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class GraphPath {
	
	final static int GRAPHSIZE = 1000;
	final int MAXCOST = 10;
	final int DEF_DEPTH = 10000;
	
	private Graph _graph;
	private int _maxDepth;
	
	public GraphPath(){
		_graph = new Graph(GRAPHSIZE);
		_graph.buildEdges(MAXCOST);
		_maxDepth = DEF_DEPTH;
	}
	
	public GraphPath(int size, int src, int dst)
	{
		_graph = new Graph(size);
		_graph.buildEdges(MAXCOST);
		_maxDepth = DEF_DEPTH;
		_graph.setSrcDst(src, dst);
	}
	
	public GraphPath(int src, int dst)
	{
		this (GRAPHSIZE,src,dst);
	}
	
	/*
	public GraphPath(int graphSize, int maxCost, int maxDepth){
		_graph = new Graph(graphSize);
		_graph.buildEdges(maxCost);
		_maxDepth = maxDepth;
	}*/

	/*
	public GraphPathResult runWithAStar() {
		return new GraphPathAStarSearch(_graph,_maxDepth).serch();
	}
	*/
	
	public GraphPath(String name) throws IOException {
		File file = new File(name);
		BufferedReader bf = new BufferedReader(new InputStreamReader (new FileInputStream(file)));
		String line;
		StringTokenizer st;
		
		st = new StringTokenizer(bf.readLine());
		
		int src = Integer.valueOf(st.nextToken());
		int dst = Integer.valueOf(st.nextToken());
		_maxDepth = Integer.valueOf(st.nextToken());
		
		//second line is index, count them and set as graph size
		int graphSize = new StringTokenizer(bf.readLine()).countTokens();
		
		_graph = new Graph(graphSize);
		int from = 0;
		while ((line=bf.readLine())!=null)
		{
			int to = 0;
			int value;
			st = new StringTokenizer(line);
			st.nextToken(); //first token is the row number, ignore it
			while (st.hasMoreTokens())
			{
				value = Integer.valueOf(st.nextToken());
				if (value>-1)
				{
					_graph.addEdge(from,to,value);
				}
				to++;
			}
			from++;
		}
		_graph.setSrcDst(src, dst);
		bf.close();
	}
	

	public void runWithAStar(GraphPathResult r) {
		new GraphPathAStarSearch(_graph,_maxDepth).search(r);
	}

	/*
	public GraphPathResult runWithGenetic() {
		//return new GraphGenticSearch(_graph,_maxDepth).search();
		return null;
	}
	*/
	
	
	public void runWithGradiant(GraphPathResult r,int iterations) {
		new GraphPathGradientSearch(_graph,_maxDepth).search(r,iterations);
	}
	

	/*
	public GraphPathResult runWithGradiant() {
		//return new GraphGradientSearch(_graph,_maxDepth).search(); 
		return null;
	}
	*/

	public void runWithGenetic(GraphPathResult r) {
		new GraphPathGenticSearch(_graph,_maxDepth).search(r);
	}

}
